package com.bingfor.easycablelink.home.Fragment.logistical;

import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import com.bingfor.easycablelink.R;
import com.bingfor.easycablelink.base.WebViewActivity;
import com.bingfor.easycablelink.databinding.HomeInfoCommonItemBinding;
import com.bingfor.easycablelink.databinding.LogisticalInfoItemBinding;
import com.bingfor.easycablelink.databinding.TopLayoutBinding;
import com.bingfor.easycablelink.home.Fragment.Cable.CableItemAdapter;
import com.bingfor.easycablelink.utils.ActivityUtil;
import com.bingfor.easycablelink.widgets.BannerLayout;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by LY on 2017/5/23.
 */

public class LogisticalAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private List<String> titles;
    private List<String> Urls = new ArrayList<>();

    public LogisticalAdapter(List<String> titles) {
        this.titles = titles;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == 0) {
            return new TopViewHolder(
                    TopLayoutBinding
                            .inflate(LayoutInflater.from(parent.getContext()), parent, false)
                            .getRoot());
        }else {
            return new CableViewHolder(
                    HomeInfoCommonItemBinding
                            .inflate(LayoutInflater.from(parent.getContext()), parent, false)
                            .getRoot());
        }
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (position == 0){
            TopViewHolder topView = (TopViewHolder) holder;
            topView.bannerLayout.setViewUrls(Urls);

            topView.bannerLayout.setOnBannerItemClickListener(new BannerLayout.OnBannerItemClickListener() {
                @Override
                public void onItemClick(int position){
                    Bundle bundle = new Bundle();
                    bundle.putString("url","https://www.baidu.com");
                    bundle.putString("title","详情");
                    ActivityUtil.getAppManager().move2NextPage(WebViewActivity.class,bundle);
                }
            });
        }else {
            HomeInfoCommonItemBinding binding = DataBindingUtil.findBinding(holder.itemView);

        }
    }

    @Override
    public int getItemCount() {
        return 4;
    }

    public class CableViewHolder extends RecyclerView.ViewHolder {
        LinearLayout mContain;
        CableItemAdapter cableItemAdapter = new CableItemAdapter();
        public CableViewHolder(View itemView) {
            super(itemView);
            mContain = (LinearLayout) itemView.findViewById(R.id.rl_contain);
            LayoutInflater layoutInflater = LayoutInflater.from(itemView.getContext());
            for (int i = 0; i < 5; i++) {
                LogisticalInfoItemBinding binding = DataBindingUtil.inflate(layoutInflater, R.layout.logistical_info_item, mContain, false);
                binding.container.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent intent = new Intent(ActivityUtil.getAppManager().currentActivity(),LogisticalListActivity.class);
                        v.getContext().startActivity(intent);
                    }
                });
                mContain.addView(binding.getRoot(),i);
            }

//            mRecyclerView.setLayoutManager(new LinearLayoutManager(itemView.getContext(), LinearLayoutManager.VERTICAL, false));
//            mRecyclerView.setAdapter(cableItemAdapter);
//            mRecyclerView.addItemDecoration(new MarginDecoration(0, 0, (int) itemView.getResources().getDimension(R.dimen.dimen_5dp), 0));
//            SnapHelper snapHelper = new GravitySnapHelper(Gravity.START);
//            snapHelper.attachToRecyclerView(mRecyclerView);
        }
    }

    public class TopViewHolder extends RecyclerView.ViewHolder {
        BannerLayout bannerLayout;

        public TopViewHolder(View itemView) {
            super(itemView);
            bannerLayout = (BannerLayout) itemView.findViewById(R.id.mBannerLayout);
        }
    }

    public void setUrls(List<String> urls) {
        Urls = urls;
        notifyDataSetChanged();
    }

    @Override
    public int getItemViewType(int position) {
        return position;
    }

}
